#include "Ilse.h"

Ilse::Ilse(Tree* Fvp, Distance* distance1)
{
	distance = distance1;
	Frst = new Forest();
	HeavyChild = Fvp->getHeavy();
	find_Ilse(Fvp);
}

// a Preorder traversal
// only if Fvp is father of Fvp1
void Ilse::find_Ilse(Tree* Fvp, bool flag)
{
	static int currSize; 
	int i;
	if(flag)
	{
		if(Fvp->getLeft()!=NULL)
		{
			currSize = Fvp->getHeavy()->getData()->getDT();
			for(i=0; !Fvp->getChild(i)->getData()->getHeavy() ; i++)
			{
				currSize += Fvp->getChild(i)->getData()->getDT();
			}
			currSize += distance->DeleteNode(Fvp);
		}
		else
		{
			currSize = distance->DeleteNode(Fvp);
		}
	}
	Frst->AddLeftTree(Fvp);
	Fvp->getData()->setDC(currSize);
	currSize -= distance->DeleteNode(Fvp);
	if (Fvp == HeavyChild)
	{
		return;
	}
	for(i=0; i<Fvp->getChildrenCount() ; i++)
	{
		find_Ilse(Fvp->getChild(i), false);
		if(Fvp->getChild(i)->getData()->getHeavy())
			break;
	}
}

Tree*  Ilse::get_Fk(int k) 
{
	return Frst->getTree(k);
}